﻿#ifndef NOSEPIECEOUMIT_H
#define NOSEPIECEOUMIT_H


#include "../NosepieceCommon/NosepieceInterface.h"

#include <QtCore/QObject>
#include <QtCore/QSettings>
#include <QtSerialPort/QtSerialPort>

extern "C"
{
NOSEPIECE_EXPORT Qly::NosepieceInterface * CreateInstance(QObject *parent);
NOSEPIECE_EXPORT void Release(Qly::NosepieceInterface * p);
}

namespace Qly {

class NOSEPIECE_EXPORT NosepieceOUMIT : public NosepieceInterface
{
public:
    explicit NosepieceOUMIT(QObject *parent = nullptr);
    ~NosepieceOUMIT() override;
    /**
     * @brief loadSettings 加载设置，同时也打开串口，开始监听
     * @param settings
     */
    void loadSettings(QSettings &settings) override;

    /**
     * @brief writeSettings 保存参数。通常在程序退出时调用。
     * @param settings
     */
    void writeSettings(QSettings &settings) override;

    void setSerialPort(QString comm, int baudRate, int dataBits, int parity, int stopBits);
    void setMaxIndex(int maxIndex);
    void open(); //连接到串口，开始监听
    void close(); //关闭串口
public slots:
    void setLens(int n) override; //暂时不支持
    void setLens(int oldLens, int newLens) override; //暂时不支持
    void forward() override;
    void reverse() override;
    void showSetupDialog() override;
private slots:
    void on_timeout();
    void on_readyRead();
private:
    void stateMachine(uint8_t data);
private:
    QSerialPort * m_pSerialPort = nullptr;
    QString m_comPort = "COM1";
    int m_baudRate = QSerialPort::Baud115200;
    int m_dataBits = QSerialPort::Data8;
    int m_parity = QSerialPort::NoParity;
    int m_stopBits = QSerialPort::OneStop;

    QTimer *m_pTimer = nullptr;
    int m_maxLensCount = 5;
    int m_state = 0;
};
}
#endif // NOSEPIECEOUMIT_H
